<script lang="ts">
export default {
  name: "occupantPersonIndex"
}
</script>
<script setup lang="ts">
import { onMounted, reactive, ref, toRefs } from 'vue';
import { Search, Plus, Edit, Refresh, Delete } from '@element-plus/icons-vue';
import { ElForm, ElMessage, ElMessageBox } from 'element-plus';
import {CasualContractQuery,CasualContract,CasualContractList} from "@/api/casual/contract/types";
import {casualContractList} from "@/api/casual/contract/index";

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const state = reactive({
  loading: false,
  // 选中ID数组
	ids:[] as number[],
  deptOptions: [],
  dialog: {
    title: '',
    visible: false
  },
  dataList: [] as CasualContract[]|any[],
  total:0,
  queryParams: {} as CasualContractQuery,
  formData: {} as CasualContract,
}); 

const {
  loading,
  dataList,
  queryParams,
  formData,
  dialog,
  total
} = toRefs(state);
function handleQuery() {
  loading.value = true;
  casualContractList(state.queryParams).then((res:any) => {
    dataList.value = res.data.list;
	total.value = res.data.total
    loading.value = false;
  });

}

function handleSelectionChange(selection: any) {
  state.ids = selection.map((item: any) => item.id);
}

/**
 * 重置
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}

/**
 * 添加
 */
function handleAdd(row: any) {
  formData.value.id = undefined;
  dialog.value = {
    title: '添加部门',
    visible: true,
  };
}

/**
 * 修改
 */
async function handleUpdate(row: any) {
  const deptId = row.id || state.ids;
  state.dialog = {
    title: '修改个人用户',
    visible: true,
  };
}

/**
 * 提交
 */
function submitForm() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      if (state.formData.id) {

      } else {

      }
    }
  });
}

/**
 * 删除
 */
function handleDelete(row: any) {
  const ids = [row.id || state.ids].join(',');
  if (!ids) {
    ElMessage.warning('请勾选删除项');
    return;
  }

  ElMessageBox.confirm(`确认删除已选中的数据项?`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {

    })
    .catch(() => ElMessage.info('已取消删除'));
}

/**
 * 关闭弹窗
 **/
function closeDialog() {
  dialog.value.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
}

onMounted(() => {
  handleQuery();
});
</script>
<template>
  <div class="app-container">
    <div class="search">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="合同名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入合同名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="合同编码" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入合同编码"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="filter-item"
            type="primary"
            :icon="Search"
            @click="handleQuery"
          >
            搜索
          </el-button>
          <el-button :icon="Refresh" @click="resetQuery"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card>
      <!--toolbar-->
      <template #header>
        <el-button type="success" :icon="Plus" @click="handleAdd"
        >新增</el-button
        >
<!--        <el-button-->
<!--          type="danger"-->
<!--          :icon="Delete"-->
<!--          @click="handleDelete"-->
<!--          :disabled="ids.length === 0"-->
<!--        >删除-->
<!--        </el-button>-->
      </template>

      <!--table-->
      <el-table
        v-loading="loading"
        :data="dataList"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="code" label="合同编码" width="80" />
        <el-table-column prop="name" label="合同名称" width="80" />
        <el-table-column prop="contractMoney" label="合同金额(元)" width="80" />
        <el-table-column prop="partyA" label="甲方" width="180" />
        <el-table-column prop="partyB" label="乙方" width="180" />
        <el-table-column prop="partyC" label="丙方" width="280" />
        <el-table-column prop="contractDate" label="合同签订日期" width="200" />
        <el-table-column prop="contractEndDate" label="合同截止日期" width="120" />
        <el-table-column prop="contractStartDate" label="合同起始日期" width="120" />
        <el-table-column label="操作" align="center" width="180" fixed="right">
          <template #default="scope">
            <el-button type="primary" link @click.stop="handleAdd(scope.row)"
            >新增
            </el-button>
            <el-button
              type="success"
              link
              @click.stop="handleUpdate(scope.row)"
            >
              编辑
            </el-button>
<!-- 
            <el-button type="danger" link @click.stop="handleDelete(scope.row)">
              删除
            </el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <!-- pagination -->
      <pagination
        v-if="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<style scoped>

</style>